package com.google.code.sip.ucenter.web.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.Role;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.RoleManager;
import com.google.code.sip.ucenter.service.UserManager;

/**
 * 用户控制器
 * 
 * @author Liuye
 * 
 */
@Controller
public class UserController {
	private static final Logger logger = LoggerFactory
			.getLogger(UserController.class);
	@Autowired
	private UserManager userManager;

	@Autowired
	private RoleManager roleManager;

	@RequestMapping("/admin/user.do")
	public ModelAndView index(HttpServletRequest request) {
		return list(request);
	}

	@RequestMapping("/admin/user!list.do")
	public ModelAndView list(HttpServletRequest request) {
		Page<User> page = new Page<User>();
		Map<String, Object> model = new HashMap<String, Object>();
		WebGridUtils.fillPage(request, page);
		String username = request.getParameter("username");
		if (StringUtils.isNotBlank(username)) {
			page.getFilters().put("username", username);
			model.put("username", username);
		}
		String nickName = request.getParameter("nickName");
		if (StringUtils.isNotBlank(nickName)) {
			page.getFilters().put("nickName", Pattern.compile(nickName + "?"));
			model.put("nickName", nickName);
		}
		String email = request.getParameter("email");
		if (StringUtils.isNotBlank(email)) {
			page.getFilters().put("email", email);
			model.put("email", email);
		}
		// 按注册日期降序
		page.addOrder("createdDate", false);
		userManager.findUser(page);
		model.put("totalRows", (int) page.getTotalCount());
		model.put("page", page);
		return new ModelAndView("admin.user.list", model);
	}

	@RequestMapping("/admin/user!list2.do")
	public ModelAndView list2(HttpServletRequest request) {
		Page<User> page = new Page<User>();
		Map<String, Object> model = new HashMap<String, Object>();
		long count = userManager.countUser();
		page.setPageSize(100);
		long pageCount = count / 100 + 1;
		for (int i = 1; i < pageCount; i++) {
			page.setPageNo(i);
			userManager.findUser(page);
			List<User> users = page.getResult();
			for (User user : users) {
				logger.info(user.getUsername());
				User user2 = userManager.getUserByName(user.getUsername());
				if (user2 == null) {
					logger.info("----");
				}
			}
		}
		model.put("totalRows", (int) count);
		model.put("page", page);
		return new ModelAndView("admin.user.list", model);
	}

	@RequestMapping("/admin/user!edit.do")
	public ModelAndView edit(@RequestParam("id") String id) {
		Map<String, Object> model = new HashMap<String, Object>();
		User user = null;
		if (StringUtils.isNotBlank(id)) {
			user = userManager.getUser(id);
		}
		model.put("user", user);
		return new ModelAndView("admin.user.edit", model);
	}

	@RequestMapping("/admin/user!save.do")
	public ModelAndView save(@ModelAttribute("user") User user,
			HttpServletRequest request) {
		User saveUser = null;
		if (StringUtils.isNotBlank(user.getId())) {
			saveUser = userManager.getUser(user.getId());
			saveUser.setUsername(user.getUsername());
			saveUser.setNickName(user.getNickName());
			saveUser.setEmail(user.getEmail());
		} else// 创建管理员
		{
			saveUser = user;
			Role role = roleManager.getRoleByName("ADMIN");
			if (role != null) {
				saveUser.setRole(role);
			}

		}
		userManager.saveUser(saveUser);

		return list(request);
	}

	@RequestMapping("/admin/user!resetView.do")
	public ModelAndView resetView(@RequestParam("id") String id) {
		Map<String, Object> model = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(id)) {
			User user = userManager.getUser(id);
			model.put("user", user);
		}
		return new ModelAndView("admin.user.resetView", model);
	}

	@RequestMapping("/admin/user!setPassword.do")
	public ModelAndView savePassword(HttpServletRequest request) {
		String id = request.getParameter("id");
		String newPwd = request.getParameter("newPassword");
		if (StringUtils.isNotBlank(id)) {
			userManager.setPassword(id, newPwd);
		}
		return list(request);
	}

	@RequestMapping("/admin/user!delete.do")
	public ModelAndView delete(@RequestParam("id") String id,
			HttpServletRequest request) {
		if (StringUtils.isNotBlank(id)) {
			userManager.deleteUser(id);
		}
		return list(request);
	}

	@RequestMapping("/admin/user!enabled.do")
	public ModelAndView enabled(@RequestParam("id") String id,
			HttpServletRequest request) {
		String enabled = request.getParameter("enabled");
		if (StringUtils.isNotBlank(id)) {
			User user = userManager.getUser(id);
			user.setEnabled(Boolean.valueOf(enabled));
			userManager.saveUser(user);
		}
		return list(request);
	}
}
